package com.xnfh.equipment.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.xnfh.common.bean.KcBean
import com.xnfh.common.bean.KcItemBean
import com.xnfh.equipment.R
import com.xnfh.equipment.adapter.KcEquipmentAdapter
import com.xnfh.equipment.databinding.ActivityKcEquipmentBinding
import com.xnfh.equipment.viewmodel.KcEquipmentViewModel
import com.xnfh.framework.base.BaseActivity
import com.xnfh.framework.base.BaseStateObserver

/**
 * @Description:
 * @Author: ziheng
 * @CreateDate: 2023/12/20 15:16
 * @UpdateUser: ziheng
 * @UpdateDate: 2023/12/20 15:16
 * @UpdateRemark: 更新说明
 */
class KcEquipmentActivity: BaseActivity<ActivityKcEquipmentBinding>() {
    override fun getLayoutID(): Int = R.layout.activity_kc_equipment
    companion object{
        fun start(context: Context,bundle: Bundle? = null){
            val intent = Intent(context, KcEquipmentActivity::class.java)
            bundle?.let { intent.putExtra("bundle",bundle) }
            context.startActivity(intent)
        }
    }

    private var viewModel: KcEquipmentViewModel? = null
    private var page = 1
    private var adapter: KcEquipmentAdapter? = null

    override fun init(savedInstanceState: Bundle?) {
        setHeaderTitle(getString(R.string.kc_equipment_list))
        mBind.kcEquipmentRecycle.layoutManager = LinearLayoutManager(this)
        adapter = KcEquipmentAdapter()
        mBind.kcEquipmentRecycle.adapter = adapter
        mBind.kcEquipmentRefresh.autoRefresh()
        mBind.kcEquipmentRefresh.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                viewModel?.requestKcList(mBind.plantingFieldSearch.text.toString(),page)
            }
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                viewModel?.requestKcList(mBind.plantingFieldSearch.text.toString(),page)
            }
        })
        mBind.plantingFieldSearch.setOnDeleteClickListener {
            mBind.kcEquipmentRefresh.autoRefresh()
        }
        mBind.plantingFieldSearch.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                mBind.kcEquipmentRefresh.autoRefresh()
            }
            val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
            imm.hideSoftInputFromWindow(v.windowToken, 0) //强制隐藏键盘
            return@setOnEditorActionListener false
        }


        adapter?.setOnItemClickListener { adapter, view, position ->
            val bundle = Bundle()
            val gatewayid = (adapter.data[position] as KcItemBean).gatewayid
            bundle.putString("id",gatewayid)
            KcEquipmentDetailActivity.start(this,bundle)
        }

    }

    override fun observe() {
        viewModel = KcEquipmentViewModel()
        viewModel?.kvList?.observe(this,object : BaseStateObserver<KcBean>(null){
            override fun getRespDataSuccess(it: KcBean) {
                super.getRespDataSuccess(it)
                mBind.kcEquipmentRefresh.let {
                    it.finishRefresh()
                    it.finishLoadMore()
                }
                if(it.total <= 0){
                    adapter?.setEmptyView(com.xnfh.common.R.layout.adapter_empty_view)
                    return
                }
                if(page == 1){
                    adapter?.setNewInstance(it.records.toMutableList())
                }else{
                    adapter?.addData(it.records)
                }
            }

            override fun getRespDataError() {
                super.getRespDataError()
                mBind.kcEquipmentRefresh.let {
                    it.finishRefresh()
                    it.finishLoadMore()
                }
            }
        })
    }
}